package work5;

/**
 * @TODO
 * @Author xu
 * 2025/2/26
 */
public class Bubble {
    public static void improvedBubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            boolean swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
            if (!swapped) {
                break;
            }
        }
    }

    public static void main(StringTest[] args) {
        int[] array = {64, 34, 25, 12, 22, 11, 90};
        System.out.println("原始数组:");
        for (int num : array) {
            System.out.print(num + " ");
        }
        System.out.println();
        improvedBubbleSort(array);
        System.out.println("排序后的数组:");
        for (int num : array) {
            System.out.print(num + " ");
        }
    }

}
